package cn.fl.customer.utils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;

import cn.cmm.base.framework.constant.BaseExceptionConstant;
import cn.cmm.base.framework.exception.BaseException;
import cn.cmm.base.framework.utils.PropertiesUtils;
import cn.cmm.base.framework.utils.StringUtils;
import cn.fl.customer.utils.FaceSaasDataStructure.UploadOptions;
import cn.fl.customer.utils.FaceSaasDataStructure.UserInfo;
import fl.customer.facade.customer.constant.CustomerConstant;
import net.sf.json.JSONObject;


/**
 * @memo :依图身份证校验(根据身份证号和姓名来验证此身份证的有效性)
 * @throws Exception 
 */
public class YiTuPersonIdentify {
	
	private static final Logger logger = Logger.getLogger(YiTuPersonIdentify.class);
	private static String yt_url=PropertiesUtils.getProperty(CustomerConstant.CUSTOMER_CUSTOMER_PROPERTIES_YT_URL);
	private static String yt_access_id =PropertiesUtils.getProperty(CustomerConstant.CUSTOMER_CUSTOMER_PROPERTIES_YT_ACCESS_ID);
	private static String yt_access_key = PropertiesUtils.getProperty(CustomerConstant.CUSTOMER_CUSTOMER_PROPERTIES_YT_ACCESS_KEY);
	
	
	
	/**
	 * @memo :依图身份证校验(根据身份证号和姓名来验证此身份证的有效性)
	 * @throws Exception 
	 */
	public static Map<String, Object> toYiTuChecking(String custNm,String custId,String pemPath) throws Exception{
		Map<String, Object> resultMap = new HashMap<>();
		resultMap.put("success", false);
		resultMap.put("img", "");
		
		UUID uuid  =  UUID.randomUUID();
		String sessionId=uuid.toString();//生成唯一会话ID
		YiTuClient yiTuclient = new YiTuClient();
		// 设置参数
		yiTuclient.setIp(yt_url);
		yiTuclient.setPemPath(pemPath);
		int mode = 3;//模式为3
		UserInfo userInfo = new UserInfo();
		userInfo.setCitizenId(custId);
		userInfo.setName(custNm);
		UploadOptions options = new UploadOptions();
		
		options.setRemoveWatermark(CustomerConstant.CST_YITU_REM_WAT_MARK);//true 去网纹 false 不去网纹
		logger.info("YiTuPersonIdentify.toYiTuChecking 开始验证:" + custNm);
		//校验发送
		String response = yiTuclient.registerUserInfo(yt_access_id, yt_access_key, sessionId, mode, userInfo, options);
		//response="{"+"message:OK,"+"rtn:-6502,"+"watermark_result:{origin_image_content:0,processed_image_content:0}"+"}";//测试
		//String response="{"+"message:OK,"+"rtn:0"+"}";//测试
	    //返回信息
		JSONObject jsonResponse = JSONObject.fromObject(response);
		logger.info("返回的验证结果:***"+jsonResponse);
		if (jsonResponse.getInt("rtn") == 0) { // 返回正常
			JSONObject waterMarkResponseJsonObject = jsonResponse.getJSONObject("watermark_result");//正式
			String originImageContent = waterMarkResponseJsonObject.getString("origin_image_content");//正式
			logger.info("网纹"+originImageContent);
			/*photo=originImageContent;
			if(removeWatermark){
				String processedImageContent = waterMarkResponseJsonObject.getString("processed_image_content");//正式
				photo=processedImageContent;
				logger.info("去网纹"+processedImageContent);
			}*/
			logger.info("YiTuPersonIdentify.toYiTuChecking 验证通过:" + custNm);
			resultMap.put("success", true);
			if(StringUtils.isEmpty(originImageContent)){
				originImageContent = "";
			}
			resultMap.put("img", originImageContent);
		}else if(jsonResponse.getInt("rtn") == 6300){
			logger.info("YiTuPersonIdentify.toYiTuChecking 身份证号和姓名不匹配:" + custNm);
			throw new BaseException(BaseExceptionConstant.BASE_EXCEPTION_CODE_601, "身份证号和姓名不匹配");
		}else if(jsonResponse.getInt("rtn") == -6304){//身份有效，但数据源中无网纹照，或者大小为空
			resultMap.put("success", true);
			resultMap.put("img", "");
		}else if(jsonResponse.getInt("rtn") == -619){//身份有效，图片请求内容过大
			resultMap.put("success", true);
			resultMap.put("img", "");
		}else if(jsonResponse.getInt("rtn") == -105){//身份有效，无法解析的图片内容
			resultMap.put("success", true);
			resultMap.put("img", "");
		}else {
			logger.info(response);
			logger.info(jsonResponse.getString("message"));
		}	
		return resultMap;
	}

}
